package com.helps.db;

import com.alibaba.druid.pool.DruidDataSource;

import javax.annotation.Resource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Database {

    private DruidDataSource datasource;

    public DruidDataSource getDatasource() {
        return datasource;
    }

    @Resource(name = "dataSource")
    public void setDatasource(DruidDataSource datasource) {
        this.datasource = datasource;
    }

    /**
     * 查询单条结果
     *
     * @param sql  执行的sql语句
     *
     */
    public Map<String,String> fetchOne(String sql) {
        return fetchAll(sql).get(0);
    }

    /**
     * 查询单条数据
     *
     * @param sql    执行的sql语句
     * @param params 参数集合
     */
    public Map<String,String> fetchOne(String sql, Map<Integer, Object> params) {
        return fetchAll(sql, params).get(0);
    }

    /**
     * 返回多条数据
     *
     * @param sql 执行的sql语句
     */
    public List<Map<String, String>> fetchAll(String sql) {
        Connection connection;
        PreparedStatement ps;
        ResultSet resultSet;
        List<Map<String, String>> result = new ArrayList<Map<String, String>>();
        try {
            connection = getDatasource().getConnection();
            ps = connection.prepareStatement(sql);
            resultSet = ps.executeQuery();
            while(resultSet.next()) {
                result.add(getResultMap(resultSet));
            }
            resultSet.close();
            connection.close();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return result;
    }

    /**
     * 返回多条数据
     *
     * @param sql    执行的sql语句
     * @param params 参数集合
     */
    public List<Map<String, String>> fetchAll(String sql, Map<Integer, Object> params) {
        Connection connection;
        PreparedStatement ps;
        ResultSet resultSet;
        List<Map<String, String>> result = new ArrayList<Map<String, String>>();
        try {
            connection = getDatasource().getConnection();
            ps = connection.prepareStatement(sql);
            int i = 1;
            for (Map.Entry<Integer, Object> param : params.entrySet()) {
                ps.setObject(param.getKey(), param.getValue());
            }
            resultSet = ps.executeQuery();
            while(resultSet.next()) {
                result.add(getResultMap(resultSet));
            }
            resultSet.close();
            connection.close();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return result;
    }

    private static Map<String, String> getResultMap(ResultSet rs)
            throws SQLException {
        Map<String, String> hm = new HashMap<String, String>();
        ResultSetMetaData rsmd = rs.getMetaData();
        int count = rsmd.getColumnCount();
        for (int i = 1; i <= count; i++) {
            String key = rsmd.getColumnLabel(i);
            String value = rs.getString(i);
            hm.put(key, value);
        }

        return hm;
    }
}
